package com.party.admin.web.dto.input.common;

import com.google.common.collect.Maps;
import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CommonInput {

    // 时间类型
    private Integer timeType;

    // 创建时间——开始
    private String createStart;

    // 创建时间——结束
    private String createEnd;

    // 发布者
    private String memberName;

    //类型（0: 进行中 1:已截止）
    private Integer timeStatus;

    private Integer limit; // 每页显示
    private String orderBy; // 排序

    private Integer activityType;

    private Integer isDate; // 1：不带时间 2：带时间

    //1 实物订单 2 积分订单
    private Integer orderType;

    public CommonInput() {
        /*this.timeStatus = 0;*/
    }

    public Integer getOrderType() {
        return orderType;
    }

    public void setOrderType(Integer orderType) {
        this.orderType = orderType;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public Integer getTimeType() {
        return timeType;
    }

    public void setTimeType(Integer timeType) {
        this.timeType = timeType;
    }

    public String getCreateStart() {
        return createStart;
    }

    public void setCreateStart(String createStart) {
        this.createStart = createStart;
    }

    public String getCreateEnd() {
        return createEnd;
    }

    public void setCreateEnd(String createEnd) {
        this.createEnd = createEnd;
    }

    public String getMemberName() {
        return memberName;
    }

    public void setMemberName(String memberName) {
        this.memberName = memberName;
    }

    public Integer getTimeStatus() {
        return timeStatus;
    }

    public void setTimeStatus(Integer timeStatus) {
        this.timeStatus = timeStatus;
    }

    public Integer getActivityType() {
        return activityType;
    }

    public void setActivityType(Integer activityType) {
        this.activityType = activityType;
    }

    /**
     * 拼接查询参数
     *
     * @param input
     * @return
     */
    public static Map<String, Object> appendParams(CommonInput input) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (input == null) {
            input = new CommonInput();
        }

        if (StringUtils.isNotEmpty(input.getCreateStart()) || StringUtils.isNotEmpty(input.getCreateEnd())) {
            input.setTimeType(null);
        }

        /******** 时间块处理 ********/
        if (input.getTimeType() != null && input.getTimeType() != 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            Date ed = calendar.getTime(); // 结束时间
            if (input.getTimeType() == 2) { // 本周内
                int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                if (day_of_week == 0) {
                    day_of_week = 7;
                }
                calendar.add(Calendar.DATE, -day_of_week + 1);
            } else if (input.getTimeType() == 3) { // 本月内
                calendar.set(Calendar.DAY_OF_MONTH, 1);
            } else if (input.getTimeType() == 4) { // 昨天
                calendar.add(Calendar.DATE, -1);
                ed = calendar.getTime();
            }
            Date sd = calendar.getTime(); // 开始时间
            String std = sdf.format(sd) + " 00:00:00";
            params.put("startDate", std);
            String end = sdf.format(ed) + " 23:59:59";
            params.put("endDate", end);
        }
        /******** 时间块处理 ********/

        /******** 时间段处理 ********/
        if (StringUtils.isNotEmpty(input.getCreateStart())) {
            if (new Integer(1).equals(input.getIsDate())) {
                params.put("c_start", input.getCreateStart() + " 00:00:00");
            } else {
                params.put("c_start", input.getCreateStart());
            }
        }
        if (StringUtils.isNotEmpty(input.getCreateEnd())) {
            if (new Integer(1).equals(input.getIsDate())) {
                params.put("c_end", input.getCreateEnd() + " 23:59:59");
            } else {
                params.put("c_end", input.getCreateEnd());
            }
        }
        /******** 时间段处理 ********/

        /******** 发布者处理 ********/
        if (StringUtils.isNotEmpty(input.getMemberName())) {
            params.put("memberName", input.getMemberName());
        }
        /******** 发布者处理 ********/
        if (null != input.getTimeStatus()) {
            params.put("timeStatus", input.getTimeStatus());
        }

        if (null != params.get("startDate")) {
            input.setCreateStart(params.get("startDate").toString().substring(0, "2018-05-04".length()));
        }
        if (null != params.get("endDate")) {
            input.setCreateEnd(params.get("endDate").toString().substring(0, "2018-05-04".length()));
        }
        if (input.getOrderType() != null) {
            params.put("orderType",input.getOrderType());
        }

        return params;
    }

    public static Map<String, Object> appendPageParams(Page page) {
        Map<String, Object> params = Maps.newHashMap();
        int offset = (page.getPage() - 1) * page.getLimit();
        params.put("offset", offset);
        params.put("limit", page.getLimit());
        return params;
    }

    /**
     * 最近7天，30天
     *
     * @param commonInput
     */
    public static void transfromDate(CommonInput commonInput) {
        String startTime = commonInput.getCreateStart();
        String endTime = commonInput.getCreateEnd();
        if (commonInput.getTimeType() == null && StringUtils.isEmpty(commonInput.getCreateStart())
                && StringUtils.isEmpty(commonInput.getCreateEnd())) {
            commonInput.setTimeType(1);
        }
        if (commonInput.getTimeType() != null) {
            String now = DateUtils.todayDate();
            endTime = DateUtils.todayDate();
            if (commonInput.getTimeType().equals(1)) {
                startTime = now;
            } else if (commonInput.getTimeType().equals(2)) {
                startTime = DateUtils.formatDate(DateUtils.addDay(now, -1));
                endTime = DateUtils.formatDate(DateUtils.addDay(now, -1));
            } else if (commonInput.getTimeType().equals(3)) {
                startTime = DateUtils.formatDate(DateUtils.addDay(now, -6));
            } else if (commonInput.getTimeType().equals(4)) {
                startTime = DateUtils.formatDate(DateUtils.addDay(now, -29));
            }
        }
        commonInput.setCreateStart(startTime);
        commonInput.setCreateEnd(endTime);
    }

    public Integer getIsDate() {
        return isDate;
    }

    public void setIsDate(Integer isDate) {
        if (null == isDate) {
            isDate = 2;
        }
        this.isDate = isDate;
    }
}
